// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Dynamic/RCROSSParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/IRCROSSService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (RCROSS);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/RCROSS/RCROSS.md>
*/

// 以下需要修改
enum FREQ4Index : std::uint8_t
{
    FREQ4Index_SID   = 1,
    FREQ4Index_TYPE1 = 2,
    FREQ4Index_ID1   = 3,
    FREQ4Index_COMP1 = 4,
    FREQ4Index_TYPE2 = 5,
    FREQ4Index_ID2   = 6,
    FREQ4Index_COMP2 = 7,
};

SG::DataStructure::Common::I64 GetTypeId (std::string type)
{
    boost::trim (type);
    std::vector<std::string> typeSort = { "DISP", "VELO", "ACCEL", "OLOAD", "SPCF", "MPCF", "STRESS", "STRAIN", "FORCE" };

    for (size_t i = 0; i < typeSort.size (); i++)
    {
        if (type == typeSort[i])
        {
            return i + 1;
        }
    }

    return 0;
}

SG::DataStructure::Common::Status BDF::BULKDATA::ParseRCROSS (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::RCROSSData tempData;

    std::string curStrKey;
    for (size_t i = 1; i < 9; i++)
    {
        curStrKey          = dividedEntryInfo[i];
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            continue;
        }

        switch (i)
        {
        case FREQ4Index_SID:
            tempData.m_sId = convertToI64 (curStrKey);
            break;
        case FREQ4Index_TYPE1:
            tempData.m_RTYPE1 = GetTypeId (curStrKey);
            break;
        case FREQ4Index_ID1:
            tempData.m_ID1 = convertToI64 (curStrKey);
            break;
        case FREQ4Index_COMP1:
            tempData.m_COMP1 = convertToI64 (curStrKey);
            break;
        case FREQ4Index_TYPE2:
            tempData.m_RTYPE2 = GetTypeId (curStrKey);
            break;
        case FREQ4Index_ID2:
            tempData.m_ID2 = convertToI64 (curStrKey);
            break;
        case FREQ4Index_COMP2:
            tempData.m_COMP2 = convertToI64 (curStrKey);
            break;
        default:
            break;
        }
    }

    if(tempData.m_RTYPE2 == 0)
    {
        tempData.m_RTYPE2 = tempData.m_RTYPE1;
    }
    // 将解析出来的数据添加到数据库中，并返回结果
    return dbServiceFactory->get<IRCROSSService> ()->Add (tempData);
}